package com.itheima.mongo;

import com.itheima.mongoservice.TanhuaServiceApi;
import com.itheima.pojo.mongo.RecommendUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Slf4j
@DubboService
public class TanhuaServiceApiImpl implements TanhuaServiceApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 今日佳人
     * @param uid
     * @return
     */
    @Override
    public RecommendUser getTodayBest(Long uid) {
        Query query = Query.query(Criteria.where("toUserId").is(uid))
                .with(Sort.by(Sort.Order.desc("score")))//降序排序
                .limit(1);//取第一个
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        log.info("今日佳人MongoDB信息：{}", recommendUser);
        return recommendUser;
    }

    @Override
    public List<RecommendUser> personalInfo(Long id) {
        Query query = Query.query(Criteria.where("toUserId").is(id));
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        log.info("推荐佳人列表：{}", list);
        return list;
    }

    @Override
    public List<RecommendUser> recommendationByPage(Integer page, Integer pagesize, Long id) {
        Query query = Query.query(Criteria.where("toUserId").is(id)).with(Sort.by(Sort.Order.desc("score")));
        return mongoTemplate.find(query, RecommendUser.class);
    }

    @Override
    public RecommendUser getRecommendationUser(Long id, Long uid) {
        Query query = Query.query(Criteria.where("userId").is(id).and("toUserId").is(uid));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }
}
